package com.wwdeng.print.app.printer

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.widget.Toast
import com.wwdeng.print.app.databinding.ActSdkPrinterBinding
import com.wwdeng.print.app.printer.PrintHelper.formatPrintHandover
import com.wwdeng.print.app.printer.PrintHelper.formatPrintInvoice
import com.wwdeng.print.app.printer.PrintHelper.formatPrintKitchen
import com.wwdeng.print.app.printer.PrintHelper.formatPrintPreSettlementOrder
import com.wwdeng.print.app.printer.PrintHelper.formatPrintSettlementOrder
import net.posprinter.posprinterface.TaskCallback

/**
 * PrintTestDemo
 * @Author：wwdeng
 * @CreateTime：2025/4/1 00:33
 * <pre>
 * 模拟打印测试
 * </pre>
 */
class PrinterSdkAct: Activity() {
    lateinit var binding: ActSdkPrinterBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActSdkPrinterBinding.inflate(layoutInflater)
        setContentView(binding.root)

        PrintSDK.initPrintService(this)
        initClickListener()
        showPrinterConnected()
    }

    private fun initClickListener(){
        //连接
        binding.btnConnect.setOnClickListener {
            if (getPrintIP().isEmpty()){
                showToast("请输入打印机IP")
                return@setOnClickListener
            }
            //已连接
            if (PrintSDK.isConnected){
                return@setOnClickListener
            }
            onConnectStartAction()
        }
        //断开
        binding.btnDisconnect.setOnClickListener {
            if (!PrintSDK.isConnected){
                showToast("未连接打印机")
                return@setOnClickListener
            }
            onDisConnectAction()
        }
        //打印预结单
        binding.btnPrint.setOnClickListener {
            if (!PrintSDK.isConnected){
                showToast("未连接打印机")
                return@setOnClickListener
            }
            onPrintPreSettlementOrderAction()
        }
        //打印结账单
        binding.btnPrint2.setOnClickListener {
            if (!PrintSDK.isConnected){
                showToast("未连接打印机")
                return@setOnClickListener
            }
            onPrintSettlementOrderAction()
        }
        //打印发票
        binding.btnPrint3.setOnClickListener {
            if (!PrintSDK.isConnected){
                showToast("未连接打印机")
                return@setOnClickListener
            }
            onPrintInvoiceAction()
        }
        //后厨单
        binding.btnPrint4.setOnClickListener {
            if (!PrintSDK.isConnected){
                showToast("未连接打印机")
                return@setOnClickListener
            }
            onPrintKitchenAction()
        }
        //交班单
        binding.btnPrint5.setOnClickListener {
            if (!PrintSDK.isConnected){
                showToast("未连接打印机")
                return@setOnClickListener
            }
            onPrintHandoverAction()
        }
        //测试
        binding.btnPrint6.setOnClickListener {
            if (!PrintSDK.isConnected){
                showToast("未连接打印机")
                return@setOnClickListener
            }
            onPrintTestAction()
        }
    }

    /**
     * 连接
     */
    private fun onConnectStartAction(){
        PrintSDK.onConnectPrintAction(getPrintIP(),object : TaskCallback{
            override fun OnSucceed() {
                showPrinterConnected()
                showToast("连接成功")
            }

            override fun OnFailed() {
                showToast("连接失败")
            }
        })
    }

    /**
     * 断开连接
     */
    private fun onDisConnectAction(){
        PrintSDK.onDisConnectPrintAction(object : TaskCallback{
            override fun OnSucceed() {
                showPrinterConnected()
                showToast("断开成功")
            }
            override fun OnFailed() {
                showPrinterConnected()
                showToast("断开失败")
            }
        })
    }

    /**
     * 设置打印机连接状态
     */
    @SuppressLint("SetTextI18n")
    private fun showPrinterConnected(){
        binding.tvConnected.text = "打印机是否连接：${PrintSDK.isConnected}"
        showPrinterConnectionStatus()
    }

    /**
     * 设置打印机状态
     */
    @SuppressLint("SetTextI18n")
    private fun showPrinterConnectionStatus(){
        when(PrintSDK.isConnected){
            true -> {
                runOnUiThread {
                    binding.tvConnectionStatus.text = "打印机状态：${PrintSDK.getPrinterStatus().toString()}"
                }
            }
            else -> {
                binding.tvConnectionStatus.text = "打印机状态：未知"
            }
        }
    }

    /**
     * 打印预结单
     */
    private fun onPrintPreSettlementOrderAction(){
        //模拟预结订单信息
        val preOrderData = TestPrintData.getPreOrderData()

        val list: MutableList<ByteArray> = ArrayList()
        list.formatPrintPreSettlementOrder(preOrderData)
        //切纸
        list.add(EscPosUtils.cutPaper())

        PrintSDK.onPrintAction(list,object: TaskCallback{
            override fun OnSucceed() {
                showToast("打印成功")
            }

            override fun OnFailed() {
                showToast("打印失败")

            }
        })
    }

    /**
     * 打印结账单
     */
    private fun onPrintSettlementOrderAction(){
        //模拟结账订单信息
        val orderData = TestPrintData.getSettlementOrderData()

        val list: MutableList<ByteArray> = ArrayList()
        list.formatPrintSettlementOrder(orderData)
        //切纸
        list.add(EscPosUtils.cutPaper())
        PrintSDK.onPrintAction(list,object: TaskCallback{
            override fun OnSucceed() {
                showToast("打印成功")
            }

            override fun OnFailed() {
                showToast("打印失败")

            }
        })
    }

    /**
     * 打印发票
     */
    private fun onPrintInvoiceAction(){
        //模拟发票信息
        val invoiceData = TestPrintData.getInvoiceData()

        val list: MutableList<ByteArray> = ArrayList()
        list.formatPrintInvoice(invoiceData)

        //切纸
        list.add(EscPosUtils.cutPaper())
        PrintSDK.onPrintAction(list,object: TaskCallback{
            override fun OnSucceed() {
                showToast("打印成功")
            }

            override fun OnFailed() {
                showToast("打印失败")

            }
        })
    }

    /**
     * 打印后厨单
     */
    private fun onPrintKitchenAction(){
        //模拟后厨单信息
        val kitchenData = TestPrintData.getKitchenData()

        val list: MutableList<ByteArray> = ArrayList()
        list.formatPrintKitchen(kitchenData)

        //切纸
        list.add(EscPosUtils.cutPaper())
        PrintSDK.onPrintAction(list,object: TaskCallback{
            override fun OnSucceed() {
                showToast("打印成功")
            }

            override fun OnFailed() {
                showToast("打印失败")

            }
        })
    }

    /**
     * 打印交班单
     */
    private fun onPrintHandoverAction(){
        //模交班单信息
        val handoverData = TestPrintData.getHandoverData()

        val list: MutableList<ByteArray> = ArrayList()
        list.formatPrintHandover(handoverData)

        //切纸
        list.add(EscPosUtils.cutPaper())
        PrintSDK.onPrintAction(list,object: TaskCallback{
            override fun OnSucceed() {
                showToast("打印成功")
            }

            override fun OnFailed() {
                showToast("打印失败")

            }
        })
    }

    //打印测试
    private fun onPrintTestAction(){
        val list: MutableList<ByteArray> = ArrayList()
//                // 初始化打印机
//                list.add(EscPosUtils.initPrinter())
//
//                // 设置居中对齐
//                list.add(EscPosUtils.setAlign(1))

//                list.add(StringUtils.strTobytes("1234567890qwertyuiopakjbdscm nkjdv mcdskjb\r\n"));
//                list.add(DataForSendToPrinterPos80.selectOrCancelBoldModel(1));
//                list.add(StringUtils.strTobytes("1234567890qwertyuiopakjbdscm nkjdv mcdskjb\r\n"));
//                list.add(DataForSendToPrinterPos80.ConvertToVietnam("Chào mừng bạn đến sử dụng máy in nhiệt\r\n"));

//                val bitmap = BitmapProcess.compressBmpByYourWidth(BitmapFactory.decodeResource(PrintApp.app.resources, R.mipmap.test_logo), 300)
//                //logo
//                val bList = BitmapProcess.cutBitmap(150,bitmap)
//                for (i in bList.indices) {
//                    list.add(DataForSendToPrinterPos80.printRasterBmp(0, bList[i], BitmapToByteData.BmpType.Dithering, BitmapToByteData.AlignType.Center, 576))
//                }
        //切纸
        list.add(EscPosUtils.cutPaper())
        PrintSDK.onPrintAction(list,object: TaskCallback{
            override fun OnSucceed() {
                showToast("打印成功")
            }

            override fun OnFailed() {
                showToast("打印失败")

            }
        })
    }

    /**
     * 获取打印机IP
     */
    private fun getPrintIP(): String{
        return binding.editIP.text.toString()
    }

    /**
     * Toast
     */
    private fun showToast(msg: String){
        Toast.makeText(this,msg, Toast.LENGTH_SHORT).show()
    }
}